#include "../../framework/inc/xDxApp.h"
#include "../../../dxhead/dxegheader.h"
#include "../inc/xMouse.h"
#include "../inc/xDxInput.h"
#include "../inc/xSprite.h"
namespace Sky
{
	using namespace Sky;

xMouse::xMouse(void):
m_bCreated(vFalse),
m_bCreateCalled(vFalse),
m_pArrow(&m_myArrow)
{
	region.left = 0;
	region.right = 1024;
	region.top = 0;
	region.bottom = 768;
	m_fSpeed=10.0f;
	m_dwPosition=0;
	m_dwFrame=0;
	m_vPos.x = 0;
	m_vPos.y = 0;
	m_vPos.z = 0;
	m_dwPresentInterval=0.0f;
	m_rSour.left=0;m_rSour.top=0;
	m_pArrow = vNull;
}

xMouse::~xMouse(void)
{
}


TResult xMouse::onCreate(xDxApp* app)
{
	D3DXMatrixIdentity( &m_Trans);
	if(m_pArrow != vNull)
	{
		return XE_OK;
	}
	if(m_bCreated)	{
		m_pArrow = &m_myArrow;
		return XE_OK;
	}
	if(!m_bCreateCalled)
	{
		m_bCreateCalled = vTrue;
		m_myArrow.setType(xPict::Type_Normal).setSrcFile(skynew (String(_XT("arrow.tga")))).setWidth(16).setHeight(16).setColorKey(D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f)).setMipLevel(1);
		myDeV_Ret_M(m_myArrow.Create(app));
		m_pArrow = &m_myArrow;
		m_bCreated = vTrue;
		return XE_OK;
	}
	if(m_bCreateCalled)
	{
		myTrace0A(M,"xMouse create allready failed");
		return XE_ALREADYERROR;
	}
	return XE_OK;
}
TResult xMouse::attachPict( xPict* pict )
{
	xAssertA(pict&&pict->isNormalTexture()&&"error in attachPict()");
	if(pict->getNormalTexture() == vNull){
		myDeV_Ret_M(pict->trySelfCreate())
	}
	m_pArrow = pict;
	return XE_OK;
}
TVoid xMouse::detachPict()
{
	m_pArrow = vNull;
}
TResult xMouse::onDelete(xDxApp* app)
{
	if(m_bCreated)
	{
		m_myArrow.Delete(app);
		m_bCreated = vFalse;
	}
	return XE_OK;
}
TResult xMouse::Render(xSprite* sprite)
{

	TResult hr = XE_OK;
	if(m_pArrow == vNull )
	{
		if(m_bCreated)	{
			m_pArrow = &m_myArrow;
		}
		else if(m_bCreateCalled){
			return XE_ALREADYERROR;
		}
		else {
			myDxV_Ret_M(trySelfCreate());
		}
	}

	sprite->getSprite()->SetTransform( &m_Trans );
	hr = sprite->getSprite()->Draw(m_pArrow->getNormalTexture(),
		NULL,
		NULL,
		&m_vPos,
		D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f) );
	myDeV_Ret_M(hr);
	return hr;
}
TVoid xMouse::SetVectorDest(D3DXVECTOR3 vPos)
{
	m_vPos=vPos;
}
TVoid xMouse::SetTransform(D3DXMATRIX& matTrans)
{

	m_Trans = matTrans;
}

TVoid xMouse::Update(TInt dltx,TInt dlty)
{
	m_vPos.x += dltx;
	m_vPos.y += dlty;
	if(m_vPos.x > region.right)m_vPos.x = region.right;
	if(m_vPos.x < region.left)m_vPos.x = region.left;
	if(m_vPos.y > region.bottom)m_vPos.y = region.bottom;
	if(m_vPos.y < region.top)m_vPos.y = region.top;

}

TVoid xMouse::SetRegion( TInt left,TInt top,TInt right,TInt bottom )
{
	region.left = left;
	region.right = right;
	region.top = top;
	region.bottom = bottom;
}

TVoid xMouse::GetMousePos( TInt* x,TInt* y)
{
	*x = m_vPos.x;
	*y = m_vPos.y;
}



}